﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PurchaseInLibrary.aspx.cs" Inherits="HQWeb.LibarySys.PurchaseInLibrary" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="../Styles/Site.css" rel="Stylesheet" type="text/css" />
     <script type="text/javascript" language="javascript">
        function checkRate(input) {
            var value = input.value;
            if (value != "") {
                var re = /^[0-9]+$/;
                if (!re.test(value)) {
                    //alert("请输入数字");
                    //input.value = "";
                    //input.focus();
                    return false;
                }
                else{
                    return true;
                }
            }
           }


             Array.prototype.contains = function(obj) {
                var i = this.length;
                while (i--) {
                    if (this[i] === obj) {
                        return true;
                    }
                }
                return false;
            }

         function AddMultiMaterial(vItemID, vItemNameHid) 
        {
            var vIDValue = document.getElementById(vItemID);
            var vNameValueHid = document.getElementById(vItemNameHid);
            
            var vURL,vHeight,vWidth;
            vURL = "../Resources/MultiMaterialList/SelectFrame.aspx?itemIDValue=" + vIDValue.value;
            vHeight = "525px";
            vWidth = "875px";
                
            var vReturn = window.showModalDialog(vURL,"元素选择","dialogWidth:"+vWidth+";dialogHeight:"+vHeight+";center:yes;help=no;resizable:no;status:no;scroll=no");
            if(vReturn==null)
            {}
            else
            {
                if(vReturn.length ==2)
                {
                     vIDValue.value = vReturn[0];
	                vNameValueHid.value = vReturn[1];
                    var NameArray = vNameValueHid.value.split(",");
                    var IDArray = new Array();
                    IDArray = vIDValue.value.split(",");
                    var exist = document.getElementById("hidMaterialIDList").value;
                    var shortexist = exist.substring(1,exist.length-1);
                    var existArray = shortexist.split(",");
                    if(existArray != ""){
                        for(var j=0;j<existArray.length;j++){
                            if(!IDArray.contains(existArray[j])){
                                var delnode = document.getElementById(existArray[j]).value;
                                DeleteNode(delnode);
                            }
                        }
                    }
                    for(var i=0;i<IDArray.length;i++){
                       AddMaterial(IDArray[i], NameArray[i]); 
                    }
	            }
            }
        }

        function AddMaterial(strid, strname) {
            //var str = window.showModalDialog("../Resources/MaterialFrame.htm", " ", "dialogWidth=800px;dialogHeight=400px");
             if (strid != "") {
                var materialList = document.getElementById("hidMaterialIDList").value;
                if (materialList.indexOf(',' + strid + ',') >= 0) {
                    //alert("添加重复项目！");
                }
                else {
                    if (materialList != null && materialList != "") {
                        document.getElementById("hidMaterialIDList").value += strid + ",";
                    } else {
                        document.getElementById("hidMaterialIDList").value = "," + strid + ",";
                    }
                    var Count = document.getElementById("hidCount").value;
                    var strHtml = "";
                    strHtml = strHtml + '  <table width="58%" id=\"table' + Count + '\">';
                    strHtml = strHtml + "<tr>";
                    strHtml = strHtml + "<td align=\"right\" style=\"width: 10%\">";
                    strHtml = strHtml + " 元素名称：";
                    strHtml = strHtml + "</td><td align=\"left\" style=\"width: 10%\">"
                    strHtml = strHtml + "<input type=\"text\" readonly=\"readonly\" onchange=\"this.setAttribute('value',this.value);\" value=\"" + strname + "\" id=\"n" + Count + "\" />";
                    strHtml = strHtml + "</td><td align=\"right\" style=\"width: 10%\">";
                    strHtml = strHtml + "申请数量：";
                    strHtml = strHtml + "</td><td align=\"left\" style=\"width: 10%\">";
                    strHtml = strHtml + "<input type=\"text\" onchange=\"this.setAttribute('value',this.value);\" id=\"s" + Count + "\" />";
        //            strHtml = strHtml + "</td><td align=\"right\" style=\"width: 10%\">";
        //            strHtml = strHtml + "库存量：";
        //            strHtml = strHtml + "</td><td align=\"left\">";
        //            strHtml = strHtml + "<input type=\"text\" onchange=\"this.setAttribute('value',this.value);\"  value=\"" + str.num + "\" readonly=\"readonly\" id=\"c" + Count + "\" />";
        //            strHtml = strHtml + "</td>";
                    strHtml = strHtml + "<td align=\"right\" style=\"width: 10%\">";
                    strHtml = strHtml + "<input type=\"button\" id=\"del" + Count + "\" value=\"删除\"  onclick=\"DeleteNode('table" + Count + "')\"/>";
                    strHtml = strHtml + "<input type=\"text\" onchange=\"this.setAttribute('value',this.value);\" style=\"display: none\" value=\"" + strid + "\" id=\"p" + Count + "\" />";
                    strHtml = strHtml + " <input type=\"text\" onchange=\"this.setAttribute('value',this.value);\" id=\"e" + Count + "\" style=\"display: none\" />";
                    strHtml = strHtml + " <input type=\"text\" onchange=\"this.setAttribute('value',this.value);\" id=\"" + strid + "\" value=\"table" + Count +"\" style=\"display: none\" />";
                    strHtml = strHtml + "</td>";
                    strHtml = strHtml + "</tr>";
                    strHtml = strHtml + "</table>";

                    document.getElementById("hidCount").value = parseInt(Count, 10) + 1;
                    document.getElementById("Addtable").innerHTML = document.getElementById("Addtable").innerHTML + strHtml;
             }
         }
     }

        //JS 删除节点
        function DeleteNode(nodeID) {
            var varType = document.getElementById("hidType").value;
            if (varType == "edit") {
                var varDelID = "e" + nodeID.substr(5);
                varDelID = document.getElementById(varDelID).value;
               // HQWeb.LibarySys.LibraryServer.DelPurchaseMaterials(varDelID, DelResout, failedCallback);
            }
            //去掉检测重复项中的id
            var materialList = document.getElementById("hidMaterialIDList").value;
            var materialID = "p" + nodeID.substr(5);
            var materialIDValue = "," + document.getElementById(materialID).value + ",";
            materialList = materialList.replace(materialIDValue, ",");
            document.getElementById("hidMaterialIDList").value = materialList;
            var IDList = "," + document.getElementById("txtUserIdHid").value + ",";//去掉元素列表中的id
            IDList = IDList.replace(materialIDValue, ",");
            //IDList = IDList.substring(1);
            IDList = IDList.substring(1,IDList.length-1);
            document.getElementById("txtUserIdHid").value = IDList;
            var NameList = document.getElementById("txtUserNameHid").value;
            var NameID = "n" + nodeID.substr(5);
            var NameIDValue = "," + document.getElementById(NameID).value + ",";
            NameList = NameList.replace(NameIDValue, ",");
            //NameList = NameList.substring(1);
            NameList = NameList.substring(1,NameList.length-1);
            document.getElementById("txtUserNameHid").value = NameList;
            var trnode = document.getElementById(nodeID);
            trnode.parentNode.removeChild(trnode);
        }

        //删除回调函数
        function DelResout(resout) {
            if (resout != "") {
                alert("resout");
            }
        }

        //保存返回结果
        function SaveResout(resout) {
            if (resout == "") {
                //Response.Redirect("ApplyList.aspx");
                document.getElementById("btnReturn").click();
                //alert("保存成功！");
                //document.getElementById("apply").disabled = true;
            } else {
                if (resout == "SessionTimeout") {
                    top.location.href = '../Login/Login.aspx';
                }
                else {
                    alert(resout);
                }
            }
        }
        //获得元素事件
        function GeArrList(type) {
            var objArry = new Array();
            var Count = document.getElementById("hidCount").value;
            var varPID = document.getElementById("hidPid").value;
            for (var i = 0; i < Count; i++) {
                var idTemp = "p" + i;
                var inputTpem = document.getElementById(idTemp);
                if (inputTpem != null && inputTpem != undefined) {
                    var obj = new Object();
                    if (inputTpem.value == "") {
                        var alter = "元素种类必选！";
                        return [alter, null];
                    } else {
                        obj.MATERIAL_ID = inputTpem.value;
                        idTemp = "s" + i;
                        inputTpem = document.getElementById(idTemp);
                        if (inputTpem.value != "") {
                            obj.NUM = inputTpem.value;
                        }
                        idTemp = "n" + i;
                        inputTpem = document.getElementById(idTemp);
                        if (inputTpem.value != "") {
                            obj.MATERIAL_NAME = inputTpem.value;
                        }
                        if (type == "edit") {
                            idTemp = "e" + i;
                            if (document.getElementById(idTemp).value != "") {
                                obj.ID = document.getElementById(idTemp).value;
                            } else {
                                obj.ID = 0;
                            }

                            obj.PURCHASE_ID = varPID;
                        }
                    }
                    objArry.push(obj);
                }

            }
            return ["", objArry]
        }

        //保存事情
        function AddData() {
            document.getElementById("apply").disabled = true;     //不可保存
            var nameInput = document.getElementById("applytitel");
            if (nameInput.value == "") {
                alert("标题不能为空！");
                nameInput.focus();
                document.getElementById("apply").disabled = false;     //可保存
                return;
            }
             var Count = document.getElementById("hidCount").value;
             for(var i = 0; i < Count; i++){
                 var idTemp1 = "s" + i;
                 var inputTemp1 = document.getElementById(idTemp1);
                  if (inputTemp1 != null && inputTemp1 != undefined)
                  {
//                 var idTemp2 = "c" + i;
//                 var inputTemp2 = document.getElementById(idTemp2).value;
                 if (inputTemp1.value == "") {
                     alert("申请数量不能为空！");
                     document.getElementById(idTemp1).focus();
                     document.getElementById("apply").disabled = false;
                     return;
                 }
                 else {
                     if (!checkRate(inputTemp1)) {
                         alert("所需数必须是整数");
                         document.getElementById(idTemp1).value = "";
                         document.getElementById(idTemp1).focus();
                         document.getElementById("apply").disabled = false;
                         return;
                     }
//                     else {
//                         if (inputTemp1.value > inputTemp2.value) {
//                             alert("申请数量不能大于当日的库存量！")
//                             document.getElementById(idTemp1).value = "";
//                             document.getElementById(idTemp1).focus();
//                             return;
//                         }
//                     }   
                 }
                 }
             }
            var varType = document.getElementById("hidType").value;
            var PurchaseModel = new Object();
            if (varType == "edit") {
                PurchaseModel.ID = document.getElementById("hidPid").value;
            }
            PurchaseModel.TITEL = nameInput.value;
            PurchaseModel.BRANCH_ID = document.getElementById("drop_branch").value;
            PurchaseModel.REMARK = document.getElementById("Remark").value;
            PurchaseModel.LIBRARY_ID = document.getElementById("droplibrary").value;
            //PurchaseModel.APPLICANT = Session["USER_ID"].toString();
            //PurchaseModel.APPLICANT_NAME =Session["USER_NAME"].toString();
            var info = GeArrList(varType);
            if (info[0] != "") {
                alert(info[0]);
                document.getElementById("apply").disabled = false;     //可保存
                return;
            } else {
                if (varType == "edit") {
                    //HQWeb.LibarySys.LibraryServer.EditPurchase(PurchaseModel, info[1], SaveResout, failedCallback);
                } else {
                    HQWeb.LibarySys.LibraryServer.AddWarehousing(PurchaseModel, info[1], SaveResout, failedCallback);
                }
            }
            document.getElementById("apply").disabled = false;     //可保存
        }

        //客户端异常处理
        function failedCallback(error) {
            alert(error.get_message());
        }


        //        function openTreeSelect(vObjID, vObjName,vObjParam) {
        //            var vURL = '../Resources/SelectFrame.aspx?MaterialID=' + vObjID + '&MaterialName=' + vObjName + '&Param=' + vObjParam;
        //            //vURL = vURL + '&objname=' + vObjName + '&objid=' + vObjID;
        //            window.open(vURL, "", "top=150px,left=200px,height=600px,width=800px");
        //        }


        function funCheckMessage() {
            var varTemp = document.getElementById("Applications");
            if (varTemp.value.replace(/(\s*$)/g, "") == "") {
                alert("申请数量不能为空！");
                varTemp.focus();
                return false;
            }
        }

        function change() {
            var branchid = document.getElementById("drop_branch").value;
            HQWeb.LibarySys.LibraryServer.BindLibrary(branchid, bindLibrary, failedCallback);
        }

        function bindLibrary(obj) {
            var selectitem = document.getElementById("droplibrary");
            selectitem.options.length
            for (var i = selectitem.options.length - 1; i >= 0; i--) {
                selectitem.options.remove(i);
               }
                for (var i in obj) {
                    var optionitem = document.createElement('OPTION');
                    optionitem.text = obj[i].LIBRARY_NAME;
                    optionitem.value = obj[i].LIBRARY_ID;
                    selectitem.options.add(optionitem);
                }
        }

    </script>
</head>
<body>
      <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
        <Services>
            <asp:ServiceReference Path="LibraryServer.asmx" />
        </Services>
    </asp:ScriptManager>
    <div>
        <table width="98%">
            <tr>
                <td colspan="4" class="title">
                    <asp:Label runat="server" ID="labTitle" Text="采购入库"></asp:Label>
                </td>
            </tr>
            <tr>
                <td align="right" style="width: 20%">
                    入库标题 ：
                </td>
                <td colspan="3">
                    <%--<asp:TextBox ID="Titel" runat="server"></asp:TextBox>--%>
                    <input type="text" runat="server" id="applytitel" /><span style="color: Red">*</span>
                </td>
            </tr>
            <tr>
                <td align="right" style="width: 20%">
                    店 名 ：
                </td>
                <td style="width: 30%">
                    <%--  <asp:DropDownList ID="BranchName" runat="server" Width="148px" onchange="change()" >
                            </asp:DropDownList>--%>
                    <select id="drop_branch" runat="server" onchange="change()" style="width: 50%;">
                    </select><span style="color: Red">*</span>
                </td>
                <td align="right" style="width: 20%">
                    仓库名 ：
                </td>
                <td style="width: 30%">
                    <%--<asp:DropDownList ID="drop_library" runat="server" Width="148px">
                            </asp:DropDownList>--%>
                    <select id="droplibrary" runat="server" style="width: 50%;">
                    </select><span style="color: Red">*</span>
                </td>
            </tr>
            <tr>
                <td align="right" style="width: 20%">
                    选择出库元素：
                </td>
                <td>
                    &nbsp;&nbsp;<%--<asp:Button runat="server" ID="btn_select" OnClientClick="AddMaterial()"
                                Text="选 择" />--%><input type="button" runat="server" value="选 择" id="ipt_select"
                                    onclick="AddMultiMaterial('txtUserIdHid','txtUserNameHid');" />
                </td>
                <td style="width: 20%" align="right">
                    入库人:
                </td>
                <td>
                    <%-- <asp:Label runat="server" ID="Applicants"></asp:Label>--%>
                    <input runat="server" type="text" id="applicant" />
                </td>
            </tr>
            <tr>
                <td colspan="4">
                    <div align="center">
                        <div id="Addtable" runat="server">
                        </div>
                    </div>
                </td>
            </tr>          
            <tr>
                <td align="right" style="width: 20%">
                    备 注：
                </td>
                <td colspan="3">
                    <%--<asp:TextBox runat="server" Width="70%" TextMode="MultiLine" ID="txtRemark"></asp:TextBox>--%>
                    <input type="text" id="Remark" runat="server" style="width: 50%;" />
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;
                </td>
                <td colspan="3">
                    &nbsp;&nbsp;<%--<asp:Button ID="btnSubmit" runat="server" Text="申 请" CommandName="add"
                                OnClick="btnSubmit_Click" OnClientClick="return funCheckMessage()" />--%><%--&nbsp;
                            <asp:Button ID="btnCancle" runat="server" Text="取 消" OnClick="btnCancle_Click" />&nbsp;--%><input
                                type="button" id="apply" value="入 库" onclick="AddData()" runat="server" />&nbsp;
                    <asp:Button ID="btnReturn" runat="server" Text="返 回" OnClick="btnReturn_Click" />
                </td>
            </tr>
            <%--                    <tr>
                        <td>
                            &nbsp;
                        </td>
                        <td colspan="3">
                            <asp:Label ID="ErrorMessage" runat="server" Text="" ForeColor="Red"></asp:Label>
                        </td>
                    </tr>--%>
        </table>
    </div>
    <div style="display: none">
        <input id="txtUserNameHid" runat="server" title="人员姓名隐藏" style="visibility: hidden" />
        <input id="txtUserIdHid" runat="server" title="人员ID隐藏" style="visibility: hidden" />
        <%--<asp:HiddenField ID="ActionType" runat="server" />--%>
        <%--<asp:HiddenField ID="ApplyID" runat="server" />--%>
        <%-- <asp:Button ID="binddrop" runat="server" OnClick="BranchName_SelectedIndexChanged" />--%>
        <asp:HiddenField ID="hidPid" runat="server" />
        <asp:HiddenField ID="hidType" runat="server" Value="add" />
        <asp:HiddenField ID="hidCount" runat="server" Value="0" />
        <asp:HiddenField ID="hidMaterialIDList" runat="server" />
        <asp:HiddenField ID="hidTypeID" runat="server" />
    </div>
    </form>
</body>
</html>
